require("dplyr")

## set working directory 
## to Dataverse folder

survey <- read.table("Harris_Data/Harris 1985 Business Week Reagan Tax Simplification Survey, study no. 851209/harris_s851209_spss.tab", header = TRUE)

### fix everything below for this survey 

# pid
survey$pid <- c(1:nrow(survey))

# study 
survey$study <- "851209"

# study year (year)
survey$year <- 1985

# geographic data (urban)
table(survey$S13)
survey$urban <- dplyr::recode(as.character(survey$S13),
                            `1` = "Central city",
                            `2` = "Suburban",
                            `3` = "Town",
                            `4` = "Rural")
table(survey$urban)

# geographic data (region)
table(survey$S11)
survey$region <- dplyr::recode(as.character(survey$S11),
                        `1` = "East",
                        `2` = "East",
                        `3` = "South",
                        `4` = "South",
                        `5` = "Midwest",
                        `6` = "Midwest",
                        `7` = "West",
                        `8` = "West")
table(survey$region)

# respondent head of household (hh)
survey$hh <- NA

# increasing inequality (inequality)
table(survey$Q7_2)
survey$inequality <- dplyr::recode(as.character(survey$Q7_2),
                            `1` = "Feel",
                            `2` = "Don't Feel",
                            `3` = "Not Sure")
table(survey$inequality)

# inequality variable (inequality.variable)
survey$inequality.variable <- 1

# union (union.self)
table(survey$F6_1)
survey$union.self <- dplyr::recode(survey$F6_1,
                            `0` = "No",
                            `1` = "Yes")
table(survey$union.self)

survey$union.other <- dplyr::recode(survey$F6_2,
                             `0` = "No",
                             `1` = "Yes")
table(survey$union.other)

table(survey$F6_4) # not sure
survey[survey$F6_4 == 1, c("union.self", "union.other", "F6_3")]

survey$union.self[survey$F6_4 == 1] <- "Not Sure"
survey$union.other[survey$F6_4 == 1] <- "Not Sure"

# employment (employed)
survey$employed <- NA

# empl self
survey$employed.self <- NA

# occupation
table(survey$F1)
survey$occupation <- dplyr::recode(survey$F1, 
                            `1` = "Professional",
                            `2` = "Manager, official",
                            `3` = "Proprietor (small business)",
                            `4` = "Clerical worker",
                            `5` = "Sales worker",
                            `6` = "Skilled craftsman, foreman",
                            `7` = "Operative, unskilled laborer (except farm)",
                            `8` = "Service worker",
                            `9` = "Farmer, farm manager, farm laborer",
                            `10` = "Student",
                            `11` = "Housewife",
                            `12` = "Military service",
                            `13` = "Unemployed",
                            `14` = "Retired",
                            `15` = "Welfare",
                            `16` = "Disabled",
                            `17` = "Other (specify)",
                            `18` = "Not sure")
table(survey$occupation)

# occ self
survey$occupation.self <- NA

# household size (hhsize)
survey$hhsize <- NA

# education (educ)
table(survey$F5)
survey$educ <- dplyr::recode(survey$F5,
                      `1` = "Less than high school",
                      `2` = "High school graduate",
                      `3` = "Some college",
                      `4` = "College graduate",
                      `5` = "Post graduate")
table(survey$educ)

# household income (income)
table(survey$F10A)
survey$income <- dplyr::recode(survey$F10A,
                        `1` = "Under $7500",
                        `2` = "$7,501 to $15,000",
                        `3` = "$15,001 to $25,000",
                        `4` = "$25,001 to $35,000",
                        `5` = "$35,001 to $50,000",
                        `6` = "$50,001 and over",
                        `7` = "Not sure/refused")
table(survey$income)

# age
table(survey$F4)
survey$age <- dplyr::recode(survey$F4,
                     `1` = "18 to 20",
                     `2` = "21 to 24",
                     `3` = "25 to 29",
                     `4` = "30 to 34",
                     `5` = "35 to 39",
                     `6` = "40 to 44",
                     `7` = "45 to 49",
                     `8` = "50 to 64",
                     `9` = "65 and over")
table(survey$age)

# race
table(survey$F12)
survey$race1 <- dplyr::recode(survey$F12,
                      `1` = "White",
                      `2` = "Black",
                      `3` = "Oriental, Asian or Pacific Islander",
                      `4` = "American Indian or Alaskan native",
                      `5` = "Not sure")
survey$race2 <- dplyr::recode(survey$F11,
                              `3` = "Decline/not sure",
                              `1` = "Yes, hispanic",
                              `2` = "No, not hispanic")
table(survey$race1)
table(survey$race2)
sum(is.na(survey$race2))

survey$race <- ifelse(survey$race1 == "Not sure" |
                        is.na(survey$race1), 
                      "Decline/not sure", ifelse(survey$race1 == "White",
                                                 ifelse(survey$race2 == "Decline/not sure" |
                                                          is.na(survey$race2),
                                                        "Decline/not sure",
                                                        ifelse(survey$race2 == "No, not hispanic",
                                                               "Non-Hispanic White",
                                                               "Hispanic White")), "Non-white"))
table(survey$race)
table(survey$race[survey$race1 == "White"])

# politics (party)
table(survey$F8A)
survey$party <- dplyr::recode(survey$F8A,
                       `1` = "Republican",
                       `2` = "Democrat",
                       `3` = "Independent",
                       `4` = "Other",
                       `5` = "Not sure")
table(survey$party)

# politics (ideology)
table(survey$F9)
survey$ideology <- dplyr::recode(survey$F9,
                          `1` = "Conservative",
                          `2` = "Middle of the road",
                          `3` = "Liberal",
                          `4` = "Not sure")
table(survey$ideology)

# gender
table(survey$S1)
survey$gender <- dplyr::recode(survey$S1,
                        `1` = "Male",
                        `2` = "Female")
table(survey$gender)

# religion
table(survey$F7A)
survey$religion <- dplyr::recode(survey$F7A,
                          `1` = "Protestant",
                          `2` = "Catholic",
                          `3` = "Jewish",
                          `4` = "Other (write in)",
                          `5` = "None",
                          `6` = "Not sure")
table(survey$religion)

#factuals
survey$factual1 <- NA
survey$factual2 <- NA
survey$factual3 <- NA

## alienation index
survey$dontcare <- dplyr::recode(survey$Q7_1,
                                 `1` = "Feel",
                                 `2` = "Don t feel",
                                 `3` = "Not sure")
survey$dontcount <- dplyr::recode(survey$Q7_3,
                                  `1` = "Feel",
                                  `2` = "Don t feel",
                                  `3` = "Not sure")
survey$leftout <- dplyr::recode(survey$Q7_4,
                                `1` = "Feel",
                                `2` = "Don t feel",
                                `3` = "Not sure")

## quesiton placement
survey$question_place <- "before party"
 
# subset
survey_851209 <-survey[,c("pid", "study", "year", "urban", "region", "hh",
                          "inequality", "inequality.variable", "union.self", "union.other",
                          "employed", "employed.self", "occupation", "occupation.self", "hhsize", "educ", "income", 
                          "age", "race", "party", "ideology", "gender", "religion",
                          "factual1", "factual2", "factual3", "dontcare", "dontcount", "leftout",
                          "question_place")]

# save file
#saveRDS(survey_851209, file = "Harris_Data/survey_851209.rds")
